<template>
    <!-- element-ui级联组件 -->
    <el-cascader
        size="mini"
        v-model="value"
        :options="treeData"
        :props="props"
        separator="-"
        @change="pushId"
    />
</template>
<script>
import { getDepartment } from '@/api/department'
import { arrToTree } from '@/utils/arrToTree'
export default {
    props: {
        value: true
    },
    data() {
        return {
            treeData: [], // 赋值给 级联组件的options
            props: {
                label: 'name', // 要展示的字段
                value: 'id', // 要存储的字段
            },
        }
    },
    created() {
        this.getDepartment()
    },
    methods: {
        async getDepartment() {
            const res = await getDepartment()
            this.treeData = arrToTree(res.data.data, 0)
        },
        pushId(val) {
            console.log(val)
            if (val.length !== 0) {
                this.$emit('input', val.reverse()[0])
            }
        },
    }
}
</script>
